/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean.validator;

import java.io.Serializable;
import java.util.Objects;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Francesco
 */
@ManagedBean ( name = "calendarNameValidator" )
public class CalendarNameValidator implements Validator , Serializable 
{

    // Static Fields
    
    /***/
    private static final FacesMessage requiredMessage = new FacesMessage ( "Name of the Calendar required !" ) ;
    
    /***/
    private static final FacesMessage nameTooLongMessage = new FacesMessage ( "Name too long !" ) ;
    
    /***/
    private static final int MAX_SIZE = 15 ;
    
    // Instance Fields
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public void validate ( FacesContext context, UIComponent component, Object value ) 
            throws ValidatorException 
    {
        String name ;
        if ( Objects.nonNull ( value ) && ! value.toString().isEmpty() )
        {
            name = value.toString () ;
            if ( name.length () > MAX_SIZE )
                throw new ValidatorException ( nameTooLongMessage ) ;
        }
        else
            throw new ValidatorException ( requiredMessage ) ;
    }
    
}
